package models

import (
	"time"
	
	"gorm.io/gorm"
)

type Category struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	Name      string    `gorm:"size:50;uniqueIndex;not null" json:"name"`
	Slug      string    `gorm:"size:50;uniqueIndex;not null" json:"slug"`
	Posts     []Post    `gorm:"foreignKey:CategoryID" json:"posts,omitempty"`
	Count     int       `json:"count"` // 用于存储分类下文章数量，不存储到数据库
}

// 钩子函数 - 创建前生成时间戳
func (c *Category) BeforeCreate(tx *gorm.DB) (err error) {
	c.CreatedAt = time.Now()
	c.UpdatedAt = time.Now()
	return
}

// 钩子函数 - 更新前更新时间戳
func (c *Category) BeforeUpdate(tx *gorm.DB) (err error) {
	c.UpdatedAt = time.Now()
	return
}